/**
 * create by ck on 2017/6/19.
 */
$(document).ready(function(){
    var $dataGrid   = new xhDataGrid($('#List'), 'Stats/getUserStatsTask'),
        $form       = new xhForm($('#xh_form')),
        $schoolDom  = $('#iSchoolId'),
        $btSummit   = $('#bt_submit'),
        checkTaskIds,
        checkTaskTimer;
    $('.xh-datagrid-toolbar').parseToolbar();//todo 更改
    $dataGrid.init().activeButton(['bt_list_del'], []);
    //完成加载处理进度条
    $dataGrid.onLoadSuccess = function(data){
        checkTaskIds = []; //初始化
        clearInterval(checkTaskTimer);
        if(data.rows.length <= 0) return false;
        $.each(data.rows, function(index, row){
            if(row.iStatus == 0){
                checkTaskIds.push(row.iTaskId);
            }
            row.iStatus = getStatusHtml(row);
            $dataGrid.list.datagrid('updateRow', {index: index, row: {iStatus: row.iStatus}});
        });
        if(checkTaskIds){
            checkTaskTimer = setInterval(function () {
                checkTaskStatus();
            }, 1000 * 3);
        }
    };
    //双击打开详情页面
    $dataGrid.onDblClickRow = function(rowIndex, rowData){
        var userType = rowData.iUserType == '学生' ? 1 : 4;
        window.top.xhTop.createNewTab('个人统计详情', baseUrl + 'Stats/userStats?taskId=' + rowData.iTaskId + '&userType=' + userType, true);
    };
    //添加
    $('#bt_list_add').click(function(){
        $form.reset();
        xhExtra.forwardToForm(false);
    });
    //返回
    $('#bt_back').click(function(){
        xhExtra.backToList();
    });
    //删除
    $('#bt_list_del').click(function(){
        if(xhExtra.buttonDisabled($(this))) return false;
        var ids = $dataGrid.getSelectValues('iTaskId');
        if(!ids) return true;
        xhExtra.askChangeStatus('Stats/delUserStatsTask', ids, $dataGrid);
    });
    //渲染学校选择框
    $schoolDom.select2({
        placeholder: "选择学校"
    });
    //执行
    $btSummit.click(function(){
        if(!$schoolDom.val()){
            window.top.xhTop.showMessage({type: 'error', title: '学校不能为空！'});
            return false;
        }
        if(!$('#content').val()){
            window.top.xhTop.showMessage({type: 'error', title: '数据列表不能为空！'});
            return false;
        }
        $btSummit.append(' <i class="fa fa-spinner fa-spin"></i>').attr('disabled', true);
        $form.submit(baseUrl + 'Stats/dealUserStats', {}, function(data){
            $btSummit.text($btSummit.text()).attr('disabled', false);
            if(!data || data.msg === 'success'){
                xhExtra.backToList($dataGrid);
                window.top.xhTop.showMessage({type: 'success', title: '执行成功！', text: '请耐心等待，稍后刷新查看结果...'});
                return false;
            }
            if(data.error){
                window.top.xhTop.showMessage({
                    title: data.error,
                    text: data.msg,
                    type: 'error'
                });
                return false;
            }
            window.top.xhTop.showMessage({
                title: '错误详情',
                text: data,
                type: 'error'
            });
        }, 3);
    });
    //检查任务状态
    function checkTaskStatus() {
        if(checkTaskIds.length <= 0) {
            clearInterval(checkTaskTimer);
            return false;
        }
        $.post(baseUrl + 'Stats/checkUserStatsTask', {ids : checkTaskIds}, function(result){
            $.each(result, function(_key, _info){
                if(_info.iStatus != 0){
                    checkTaskIds.remove(_info.iTaskId);
                }
                var index = $dataGrid.list.datagrid('getRowIndex', _info.iTaskId);
                _info.iStatus = getStatusHtml(_info);
                $dataGrid.list.datagrid('updateRow', {index:index, row: _info});
            });
        }, 'json');
    }
    //根据信息返回状态html
    function getStatusHtml(_info){
        var status = '', width = 0;
        if(_info.iStatus == 1){
            status = '<div class="progress"><div class="progress-bar progress-bar-success" style="width: 100%;"></div></div>';
        }
        if(_info.iStatus == 0){
            width = _info.iCompleteNum / _info.iStatsNum * 100;
            width = width || 1;
            status = '<div class="progress progress-striped active"><div class="progress-bar progress-bar-success" style="width: ' + width + '%;"></div></div>';
        }
        if(_info.iStatus == 2){
            status = '<div class="progress"><div class="progress-bar progress-bar-warning" style="width: 100%;"></div></div>';
        }
        if(_info.iStatus == 3){
            width = _info.iCompleteNum / _info.iStatsNum * 100;
            width = width || 1;
            status = '<div class="progress"><div class="progress-bar progress-bar-danger" style="width: ' + width + '%;"></div></div>';
        }
        return status;
    }
});
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};